<!DOCTYPE html>
<html>

<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
	<meta name="theme-color" content="#33474d">
	<title>zabbix通过自定义脚本监控nginx，php-fpm和mysql占用内存数和进程的个数 | 失落的乐章</title>
	<link rel="stylesheet" href="/css/style.css" />
	
      <link rel="alternate" href="/atom.xml" title="失落的乐章" type="application/atom+xml">
    
</head>

<body>

	<header class="header">
		<nav class="header__nav">
			
				<a href="/archives" class="header__link">Archive</a>
			
				<a href="/tags" class="header__link">Tags</a>
			
				<a href="/atom.xml" class="header__link">RSS</a>
			
		</nav>
		<h1 class="header__title"><a href="/">失落的乐章</a></h1>
		<h2 class="header__subtitle">技术面前，永远都是学生。</h2>
	</header>

	<main>
		<article>
	
		<h1>zabbix通过自定义脚本监控nginx，php-fpm和mysql占用内存数和进程的个数</h1>
	
	<div class="article__infos">
		<span class="article__date">2017-10-12</span><br />
		
		
			<span class="article__tags">
			  	<a class="article__tag-link" href="/tags/Zabbix/">Zabbix</a>
			</span>
		
	</div>

	

	
		<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;zabbix通过自定义脚本监控nginx，php-fpm和mysql占用内存数和进程的个数</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;首先，在zabbix脚本目录下添加一个脚本，写入如下代码</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div><div class="line">7</div><div class="line">8</div><div class="line">9</div><div class="line">10</div><div class="line">11</div><div class="line">12</div><div class="line">13</div><div class="line">14</div><div class="line">15</div><div class="line">16</div><div class="line">17</div><div class="line">18</div><div class="line">19</div><div class="line">20</div><div class="line">21</div><div class="line">22</div><div class="line">23</div><div class="line">24</div><div class="line">25</div><div class="line">26</div><div class="line">27</div><div class="line">28</div><div class="line">29</div><div class="line">30</div><div class="line">31</div><div class="line">32</div><div class="line">33</div><div class="line">34</div><div class="line">35</div><div class="line">36</div><div class="line">37</div><div class="line">38</div><div class="line">39</div><div class="line">40</div><div class="line">41</div><div class="line">42</div><div class="line">43</div><div class="line">44</div><div class="line">45</div><div class="line">46</div><div class="line">47</div><div class="line">48</div><div class="line">49</div><div class="line">50</div><div class="line">51</div><div class="line">52</div></pre></td><td class="code"><pre><div class="line"><span class="meta">#!/bin/bash</span></div><div class="line"><span class="comment">#license:GPL</span></div><div class="line"><span class="comment">#mail:admin@huxianglin.cn</span></div><div class="line"><span class="comment">#date:2015.04.16</span></div><div class="line">top -bn1&gt;/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/process.log</div><div class="line">LOG=/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/process.log</div><div class="line"><span class="function"><span class="title">php_fpm</span></span>()&#123;</div><div class="line">	grep <span class="string">"php-fpm"</span> <span class="variable">$LOG</span> |awk <span class="string">'&#123;sum+=$6&#125;; END&#123;print sum&#125;'</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">php_fpm_num</span></span>()&#123;</div><div class="line">        grep <span class="string">"php-fpm"</span> <span class="variable">$LOG</span> |wc -l</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">nginx</span></span>()&#123;</div><div class="line">        grep <span class="string">"nginx"</span> <span class="variable">$LOG</span> |awk <span class="string">'&#123;sum+=$6&#125;; END&#123;print sum&#125;'</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">nginx_num</span></span>()&#123;</div><div class="line">        grep <span class="string">"nginx"</span> <span class="variable">$LOG</span> |wc -l</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">mysqld</span></span>()&#123;</div><div class="line">        grep <span class="string">"mysqld"</span> <span class="variable">$LOG</span> |awk <span class="string">'&#123;sum+=$6&#125;; END&#123;print sum&#125;'</span></div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="function"><span class="title">mysqld_num</span></span>()&#123;</div><div class="line">        grep <span class="string">"mysqld"</span> <span class="variable">$LOG</span> |wc -l</div><div class="line">&#125;</div><div class="line"></div><div class="line"><span class="keyword">case</span> <span class="string">"<span class="variable">$1</span>"</span> <span class="keyword">in</span></div><div class="line">php_fpm)</div><div class="line">	php_fpm</div><div class="line">	;;</div><div class="line">php_fpm_num)</div><div class="line">	php_fpm_num</div><div class="line">	;;</div><div class="line">nginx)</div><div class="line">	nginx</div><div class="line">	;;</div><div class="line">nginx_num)</div><div class="line">	nginx_num</div><div class="line">	;;</div><div class="line">mysqld)</div><div class="line">	mysqld</div><div class="line">	;;</div><div class="line">mysqld_num)</div><div class="line">	mysqld_num</div><div class="line">	;;</div><div class="line">*)</div><div class="line"><span class="built_in">echo</span> <span class="string">"Usage: <span class="variable">$0</span> &#123;php_fpm|php_fpm_num|nginx|nginx_num|mysqld|mysqld_num&#125;"</span></div><div class="line"><span class="keyword">esac</span></div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;保存之后修改其属组和属主为zabbix.zabbix,然后新建一个文件process.log，同样设置属主和属组为zabbix.zabbix</p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;然后编辑zabbix_agentd.conf文件，在最后添加上下面这段代码，并重启zabbix_agentd服务</p>
<figure class="highlight bash"><table><tr><td class="gutter"><pre><div class="line">1</div><div class="line">2</div><div class="line">3</div><div class="line">4</div><div class="line">5</div><div class="line">6</div></pre></td><td class="code"><pre><div class="line">UserParameter=process.php_fpm,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/processstatus.sh php_fpm</div><div class="line">UserParameter=process.php_fpm_num,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/processstatus.sh php_fpm_num</div><div class="line">UserParameter=process.nginx,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/processstatus.sh nginx</div><div class="line">UserParameter=process.nginx_num,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/processstatus.sh nginx_num</div><div class="line">UserParameter=process.mysqld,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/processstatus.sh mysqld</div><div class="line">UserParameter=process.mysqld_num,/usr/<span class="built_in">local</span>/zabbix-2.4.4/scripts/processstatus.sh mysqld_num</div></pre></td></tr></table></figure>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;之后就可以在web页面添加item，生成图表了，注意top取到的内存单位是KB，所以在定义item的时候需要自定义单位和设定倍数</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E9%80%9A%E8%BF%87%E8%87%AA%E5%AE%9A%E4%B9%89%E8%84%9A%E6%9C%AC%E7%9B%91%E6%8E%A7nginx%EF%BC%8Cphp-fpm%E5%92%8Cmysql%E5%8D%A0%E7%94%A8%E5%86%85%E5%AD%98%E6%95%B0%E5%92%8C%E8%BF%9B%E7%A8%8B%E7%9A%84%E4%B8%AA%E6%95%B0/01.jpeg?raw=true" alt=""></figure></p>
<p>&#160;&#160;&#160;&#160;&#160;&#160;&#160;&#160;生成的各个程序占用内存大小的图形如下</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E9%80%9A%E8%BF%87%E8%87%AA%E5%AE%9A%E4%B9%89%E8%84%9A%E6%9C%AC%E7%9B%91%E6%8E%A7nginx%EF%BC%8Cphp-fpm%E5%92%8Cmysql%E5%8D%A0%E7%94%A8%E5%86%85%E5%AD%98%E6%95%B0%E5%92%8C%E8%BF%9B%E7%A8%8B%E7%9A%84%E4%B8%AA%E6%95%B0/02.jpeg?raw=true" alt=""></figure></p>
<p>生成的各个程序的进程个数图形如下，可以设定触发器，当进程满足触发其条件时发送报警</p>
<p><figure class="figure"><img src="https://github.com/hcldirgit/image/blob/master/zabbix%E9%80%9A%E8%BF%87%E8%87%AA%E5%AE%9A%E4%B9%89%E8%84%9A%E6%9C%AC%E7%9B%91%E6%8E%A7nginx%EF%BC%8Cphp-fpm%E5%92%8Cmysql%E5%8D%A0%E7%94%A8%E5%86%85%E5%AD%98%E6%95%B0%E5%92%8C%E8%BF%9B%E7%A8%8B%E7%9A%84%E4%B8%AA%E6%95%B0/03.jpeg?raw=true" alt=""></figure></p>

	

	
		<span class="different-posts"><a href="/2017/10/12/Zabbix/25. zabbix通过自定义脚本监控nginx，php-fpm和mysql占用内存数和进程的个数/" onclick="window.history.go(-1); return false;">⬅️ Go back </a></span>

	

</article>

	</main>

	<footer class="footer">
	<div class="footer-content">
		
	      <div class="footer__element">
	<p>Hi there, <br />welcome to my Blog glad you found it. Have a look around, will you?</p>
</div>

	    
	      <div class="footer__element">
	<h5>Check out</h5>
	<ul class="footer-links">
		<li class="footer-links__link"><a href="/archives">Archive</a></li>
		
		  <li class="footer-links__link"><a href="/atom.xml">RSS</a></li>
	    
		<li class="footer-links__link"><a href="/about">about page</a></li>
		<li class="footer-links__link"><a href="/tags">Tags</a></li>
		<li class="footer-links__link"><a href="/categories">Categories</a></li>
	</ul>
</div>

	    

		<div class="footer-credit">
			<span>© 2017 失落的乐章 | Powered by <a href="https://hexo.io/">Hexo</a> | Theme <a href="https://github.com/HoverBaum/meilidu-hexo">MeiliDu</a></span>
		</div>

	</div>


</footer>



</body>

</html>
